package buaa.com.crazzyguess;

import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import buaa.com.adapter.SelectAdapter;
import buaa.com.util.CrazyDao;
import buaa.com.util.ImageUtil;
import buaa.com.view.MyView;

public class GameActivity extends AppCompatActivity {
    private Button returnBtn;
    private TextView gameTopStar;
    private LinearLayout gameTopBar;
    private TextView coin;
    private TextView coinCount;
    private TextView delete;
    private TextView hint;
    private TextView quetsionType;
    private TextView questionImg;
    private GridView selectGrid;
    private MyView answerView;
    private List<View> allViews = new ArrayList<>();
    //取得从传过来的id
    private int id;
    private Map<String,Object> crazyMap;
    private SelectAdapter selectAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        returnBtn = (Button) findViewById(R.id.return_btn);
        gameTopBar = (LinearLayout) findViewById(R.id.game_top_bar);
        gameTopStar = (TextView) findViewById(R.id.game_top_star);
        coin = (TextView) findViewById(R.id.coin);
        coinCount = (TextView) findViewById(R.id.coin_count);
        delete = (TextView) findViewById(R.id.delete);
        hint = (TextView) findViewById(R.id.hint);
        quetsionType = (TextView) findViewById(R.id.question_type);
        questionImg = (TextView) findViewById(R.id.question_img);
        selectGrid = (GridView) findViewById(R.id.select_grid);
        answerView = (MyView) findViewById(R.id.answer_view);

        returnBtn.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getReturnBtn()));
        gameTopBar.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getTopBar()));
        gameTopStar.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getTopStar()));
        coin.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getCoin()));
        delete.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getDelete()));
        hint.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getHint()));
        quetsionType.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getQuestionType()));

        id = getIntent().getIntExtra("id",1);
        crazyMap = CrazyDao.getDataByid(id);

        gameTopStar.setText(id+"");
        coinCount.setText(String.valueOf(getSharedPreferences("game",MODE_PRIVATE).getInt("coin",50)));
        quetsionType.setText(crazyMap.get("questionType").toString());
        try {
            Log.d("QuestionImage....",crazyMap.get("questionImg").toString());
            questionImg.setBackgroundDrawable(new BitmapDrawable(getResources(), BitmapFactory.decodeStream(getAssets().open(crazyMap.get("questionImg").toString()))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String select = crazyMap.get("selectTxt").toString();
        List<Character> charList = new ArrayList<>();
        for( int i = 0 ; i < select.length() ; i++){
            charList.add(select.charAt(i));
        }
        //打乱顺序
        Collections.shuffle(charList);

        LayoutInflater lf = LayoutInflater.from(this);
        for( int i = 0 ; i < charList.size() ; i++){
            View v = lf.inflate(R.layout.select_list,null);
            TextView textView = (TextView)v.findViewById(R.id.select_txt);
            textView.setText(charList.get(i).toString());
            textView.setBackgroundDrawable(new BitmapDrawable(getResources(), ImageUtil.getCanSelect()));
            allViews.add(v);
        }
        selectAdapter = new SelectAdapter(this,allViews);
        selectGrid.setAdapter(selectAdapter);

        answerView.initAnswer(crazyMap.get("answer").toString());
        selectGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                View v = allViews.get(position);
                TextView t = (TextView) v.findViewById(R.id.select_txt);
                answerView.initUserAnswer(t.getText().toString(),position);
                v.setVisibility(View.INVISIBLE);
            }
        });
    }

    public List<View> getAllViews() {
        return allViews;
    }

}
